﻿using System;
using System.Collections.Generic;
using System.IO;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;

namespace Besture.UI
{
    public partial class WebTemplateSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
			
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id == 0) return;

            var dac = new TemplateDac();
            var model = dac.GetModel(id);

            txtName.Text = model.Name.ToString();
			txtTemplateName.Text = model.TemplateName.ToString();
			txtTemplateContent.Text = model.TemplateContent.ToString();
			

			
        }
        #endregion

        #region 保存

        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {
            var dac = new TemplateDac();
            TemplateModel model=null;
            var baseDac = new BaseDac<TemplateModel>();
            //添加
            if (!EditPage)
            {
                model = new TemplateModel();
                model.Name = txtName.Text.Trim();
				model.TemplateName = txtTemplateName.Text.Trim();
				model.TemplateContent = txtTemplateContent.Text.Trim();
                //加载添加基本数据
                baseDac.LoadAddBaseModel(model);
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(ID);
                //File.Delete(Server.MapPath("/Templates/Master/" + model.TemplateName + ".shtml"));
                model.Name = txtName.Text.Trim();
				model.TemplateName = txtTemplateName.Text.Trim();
				model.TemplateContent = txtTemplateContent.Text.Trim();
                //加载修改基本数据
                baseDac.LoadUpdateBaseModel(model);
                dac.Update(model);
            }
            Dictionary<string,string> replace=new Dictionary<string, string>();
            replace.Add("[model]",model.TemplateContent);
            new GenerateHtml().Universal(Server.MapPath("/Templates/Master/Module/model.shtml"),
                Server.MapPath("/Templates/Master/" + model.TemplateName + ".shtml"), replace);
            FunHelper.Alert("保存成功", ListUrl());
        }
        #endregion
    }
}